<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加客服</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__CSS__/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__CSS__/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="__CSS__/animate.min.css" rel="stylesheet">
    <link href="__JS__/layui/css/layui.css" rel="stylesheet">
    <link href="__CSS__/style.min.css?v=4.1.0" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-8">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>添加客服</h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t layui-form" id="commentForm" method="post" action="{:url('users/adduser')}">
                            <input type="hidden" name="user_id" value="{$user_id}">
                            <div class="form-group">
                                <label class="col-sm-3 control-label">客服所属(部门/门店)：</label>
                                <div class="input-group col-sm-4">
                                    <input id="servicename" type="text" class="form-control" name="service_name" required aria-required="true" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">负责人：</label>
                                <div class="input-group col-sm-4">
                                    <input id="username" type="text" class="form-control" placeholder="为空则在绑定时设置" name="user_name" aria-required="true" value="{$user_name}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">手机号：</label>
                                <div class="input-group col-sm-4">
                                    <!-- <input id="mobile" lay-verify="required|phone" autocomplete="off" name="mobile" required="" value="{$user_mobile}"> -->
                                    <input type="tel" class="form-control layui-input" name="mobile" lay-verify="required|phone" value="{$user_mobile}">
                                </div>
                            </div>
                            <input type="hidden" name="user_pwd" value="123456">

                            <!-- <div class="form-group">
                                <label class="col-sm-3 control-label">登录密码：</label>
                                <div class="input-group col-sm-4">
                                    <input id="password" type="text" class="form-control" name="user_pwd" required=""
                                        aria-required="true">
                                </div>
                            </div> -->
                            <div class="form-group">
                                <label class="col-sm-3 control-label">微信openid：</label>
                                <div class="input-group col-sm-4">
                                    {if($user_openid)}
                                    <input id="openid" type="text" class="form-control" name="openid" required="" aria-required="true" placeholder="" value="{$user_openid}" readonly> {else}
                                    <select lay-verify="required" lay-filter="openid" name="openid" id="openid_select" lay-search="">
                                        <option value="">从微信用户中搜索：</option>
                                        {:oauthUserOption()}
                                    </select> {/if}
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">选择人员类型：</label>
                                <div class="input-group col-sm-4 layui-form">
                                    <select name="role" lay-verify="required" lay-filter="role" id="role_select">
                                        <option value="">选择客服类型：</option>
                                        {foreach name="role" key="id" item="vo"}
                                        <option value="{$id}" {if $id eq $selected_role} selected {/if}> {$vo}</option>
                                        {/foreach}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group layui-form-item">
                                <label class="col-sm-3 control-label">是否启用：</label>
                                <div class="input-group col-sm-6">
                                    {if !empty($status)} {foreach name="status" item="vo" key="key"}
                                    <input type="radio" name="status" value="{$key}" title="{$vo}" {if $key eq 1} checked {/if}> {/foreach} {/if}
                                </div>
                            </div>
                            <div class="form-group layui-form-item form-inline" style="height: 60px;">
                                <input type="hidden" name="user_avatar" id="user_avatar" value="{$user_avatar}" />
                                <label class="col-sm-3 control-label">客服头像：</label>
                                <div class="input-group col-sm-2">
                                    <button type="button" class="layui-btn layui-btn-small" id="up-avatar">
                                        <i class="layui-icon"></i>上传图片
                                    </button>
                                </div>
                                <div class="input-group col-sm-3" id="avatar">
                                    {if($user_avatar)} <img src="{$user_avatar}" width="50px" height="50px"> {/if}
                                </div>
                            </div>
                            <div class="form-group layui-form-item" id="superior_select">
                                <label class="col-sm-3 control-label">设置客服上级</label>
                                <!-- <input type="hidden" id="superior_id" value="" /> -->
                                <div class="layui-input-inline">
                                    <select lay-verify="required" lay-filter="superior" name="superior_id" id="superior" lay-search="">
                                        <option value="">直接选择或搜索选择</option>
                                        {foreach name="superior" key="id" item="vo"}
                                        <option value="{$id}" {if $id eq $superior_id} selected {/if}> {$vo}</option>
                                        {/foreach}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-6">
                                    <button class="btn btn-primary" type="submit">提交</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <script src="__JS__/jquery.min.js?v=2.1.4"></script>
    <script src="__JS__/bootstrap.min.js?v=3.3.6"></script>
    <script src="__JS__/content.min.js?v=1.0.0"></script>
    <script src="__JS__/plugins/validate/jquery.validate.min.js"></script>
    <script src="__JS__/plugins/validate/messages_zh.min.js"></script>
    <script src="__JS__/plugins/layer/layer.min.js"></script>
    <script src="__JS__/layui/layui.js"></script>
    <script src="__JS__/jquery.form.js"></script>
    <script type="text/javascript">
        layui.use(['form', 'upload'], function() {
            var form = layui.form;
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                //绑定元素
                elem: '#up-avatar',
                //上传接口
                url: "{:url('users/upAvatar')}",
                exts: 'png|jpg|jpeg|gif',
                done: function(res) {
                    //上传完毕回调
                    if (0 == res.code) {
                        $("#avatar").html('<img src="' + res.data.src + '" width="50px" height="50px">');
                        $("#user_avatar").val(res.data.src);
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function() {
                    //请求异常回调
                }
            });

            form.on('select(role)', function(value) {
                appendSuperior(value.value);
            });
        });

        function appendSuperior(role) {
            $('#superior').children('option').remove();
            $.getJSON(
                "{:url('admin/users/superior')}", {
                    "role": role
                },
                (data) => {
                    let superior_id = '{$superior_id}';
                    $.each(data, (i, v) => {
                        let option = new Option(v, i);
                        if (i == superior_id) {
                            option.selected = true;
                        }
                        $('#superior').append(option)
                    })
                    layui.use(['form'], () => layui.form.render('select'));
                }
            )
        }
        var index = '';

        function showStart() {
            index = layer.load(0, {
                shade: false
            });
            return true;
        }

        function showSuccess(res) {
            layer.ready(function() {
                layer.close(index);
                if (1 == res.code) {
                    layer.alert(res.msg, {
                        title: '友情提示',
                        icon: 1,
                        closeBtn: 0
                    }, function() {
                        window.location.href = res.data;
                    });
                } else if (111 == res.code) {
                    window.location.reload();
                } else {
                    layer.msg(res.msg, {
                        anim: 6
                    });
                }
            });
        }

        $(document).ready(function() {
            // 添加管理员
            var options = {
                beforeSubmit: showStart,
                success: showSuccess
            };

            $('#commentForm').submit(function() {
                $(this).ajaxSubmit(options);
                return false;
            });

            {if !is_null($selected_role)} appendSuperior("{$selected_role}"); {/if}
        });

        // 表单验证
        $.validator.setDefaults({
            highlight: function(e) {
                $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
            },
            success: function(e) {
                e.closest(".form-group").removeClass("has-error").addClass("has-success")
            },
            errorElement: "span",
            errorPlacement: function(e, r) {
                e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
            },
            errorClass: "help-block m-b-none",
            validClass: "help-block m-b-none"
        });
    </script>
</body>

</html>